package com.hb.trade.server.center.au.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.center.au.dto.OrgCollectionDTO;
import com.hb.core.pojo.center.au.entity.OrgCollection;
import com.hb.trade.server.center.au.service.OrgCollectionService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 企业收款方式表 前端控制器
 * </p>
 *
 * @author zhongdl
 * @since 2022-01-15
 */
@RestController
public class OrgCollectionController {

    protected final OrgCollectionService orgCollectionService;

    public OrgCollectionController(OrgCollectionService orgCollectionService) {
        this.orgCollectionService = orgCollectionService;
    }

    /**
     *
     * @Description: 保存收款方信息
     * @Author: zhongdl
     * @Date:2022/1/15 17:08
     * @param orgCollection:
     * @return: {@link Message < Boolean> }
     **/
    @PostMapping("/orgCollection/saveOrgCollection")
    public Message<Boolean> saveOrgCollection(@RequestBody OrgCollectionDTO orgCollection){
        String msg = orgCollection.paramsValid("payee");
        if (StrUtil.isNotEmpty(msg)){
           return MsgUtil.fail(msg);
        }

        if(orgCollection.getId() == null){
            orgCollection.buildBaseData(1);
        }else {
            orgCollection.buildBaseData(2);
        }
        return MsgUtil.success(orgCollectionService.saveOrUpdate(orgCollection));
    }


    /**
     *
     * @Description: 根据id查询收款码详情
     * @Author: zhongdl
     * @Date:2022/1/17 14:16
     * @param orgCollectionDTO:
     * @return: {@link Message< OrgCollection> }
     **/
    @PostMapping("/orgCollection/getOrgCollectionId")
    public Message<OrgCollection> getOrgCollectionId(@RequestBody OrgCollectionDTO orgCollectionDTO){
        OrgCollection orgCollection = orgCollectionService.getById(orgCollectionDTO.getId());
        return MsgUtil.msg(orgCollection);
    }


    /**
     *
     * @Description: 根据企业id和收款方 查询企业的收款信息
     * @Author: zhongdl
     * @Date:2022/2/7 14:15
     * @param orgId:
     * @param payee:
     * @return: {@link Message<  OrgCollection > }
     **/
    @PostMapping("/orgCollection/getOrgCollectionOrgIdAndPayee")
    public Message<OrgCollection> getOrgCollectionOrgIdAndPayee(@RequestParam Long orgId, @RequestParam String payee ){
        QueryWrapper<OrgCollection> wrapper = OrgCollection.wrapper();
        wrapper.eq("org_id",orgId).eq("payee",payee).last("limit 1");
        OrgCollection orgCollection = orgCollectionService.getOne(wrapper);
        return MsgUtil.success(orgCollection);
    }


    /**
     *
     * @Description: 根据id删除收款方
     * @Author: zhongdl
     * @Date:2022/2/10 17:08
     * @param orgCollection:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/orgCollection/removeOrgCollectionId")
    public Message<Boolean> removeOrgCollectionId(@RequestBody OrgCollectionDTO orgCollection){
        if (orgCollection.getId() == null){
            return MsgUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        orgCollection.buildBaseData(3);
        boolean flag = orgCollectionService.updateById(orgCollection);
        return MsgUtil.success(flag);
    }
}
